<template>
  <a-modal
    :maskClosable="false"
    :title="title"
    centered
    :width="600"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="名称">
        <a-input
          placeholder="请输入名称"
          disabled
          v-decorator="['name', { rules: [{ required: true, message: '请输入名称' }] }]"
        />
        <a-input v-decorator="['id']" hidden></a-input>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="排序">
        <a-input-number
          placeholder="排序"
          :min="0"
          v-decorator="['sort', { rules: [{ required: true, message: '请输入排序' }] }]"
        />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
        <a-input type="textarea" placeholder="请输入备注" v-decorator="['remark']" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { memberTypeAdd, memberTypeEdit } from '@/api/system/system'
import pick from 'lodash.pick'
export default {
  components: {},
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      title: '',
      type: ''
    }
  },
  beforeCreate() {},
  created() {},
  methods: {
    add(obj) {
      this.title = obj.title
      this.type = obj.type
      this.visible = true
      this.$nextTick(() => {
        this.form.resetFields()
      })
    },
    edit(obj) {
      this.title = obj.title
      this.type = obj.type
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(obj.data, 'id', 'name', 'sort', 'remark'))
      })
    },

    handleSubmit(e) {
      var _this = this
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          var data = JSON.parse(JSON.stringify(values))
          if (_this.type === 'add') {
            return memberTypeAdd(data)
              .then(res => {
                _this.$message.success('新增成功', 1)
                _this.visible = false
                _this.$emit('add')
              })
              .finally(() => {
                _this.confirmLoading = false
              })
          } else {
            return memberTypeEdit(data)
              .then(res => {
                _this.$message.success('编辑成功', 1)
                _this.visible = false
                _this.$emit('upd')
              })
              .finally(() => {
                _this.confirmLoading = false
              })
          }
        }
      })
    }
  }
}
</script>
<style scoped>
.tree {
  max-height: 400px;
  overflow: auto;
}
</style>
